import {AfterViewInit, Component, EventEmitter, OnDestroy, OnInit, Output} from '@angular/core';
import {Message} from '../../../../../../ng-tools/src/message/message';
import {DynamicComponent, DynamicComponentEvent} from '../../../../../../ng-tools';
import {Subscription} from 'rxjs';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';

@Component({
  selector: 'change-dvb-config-channel',
  templateUrl: './change-dvb-config-channel.component.html',
  styleUrls: ['./change-dvb-config-channel.component.scss']
})
export class ChangeDvbConfigChannelComponent implements OnInit, AfterViewInit, OnDestroy, Message, DynamicComponent {
  context: any;
  // data: any;
  currentChannel;
  dynamic: true;
  events: DynamicComponentEvent;
  subscriptions: Subscription;
  dvbConfigChannelForm: FormGroup;
  defaultDvbConfgChannelFormValue;
  @Output() success = new EventEmitter();
  set data(value) {
    this.currentChannel = value;
    console.log(this.currentChannel);
  }
  constructor(private formBuilder: FormBuilder) {
    this.dvbConfigChannelForm = this.formBuilder.group({
      name: ['', Validators.required],
      ip: ['', Validators.required],
      port: ['', Validators.required],
      service_id: ['', Validators.required],
      oscam: ['', Validators.required],
    });
    this.defaultDvbConfgChannelFormValue = {name: '',
      ip: '',
      port: '',
      service_id: '',
      oscam: 0};
    this.dvbConfigChannelForm.patchValue(this.defaultDvbConfgChannelFormValue);
  }

  ngOnInit() {
    this.defaultDvbConfgChannelFormValue = {name: this.currentChannel.name,
      ip: this.currentChannel.ip,
      port: this.currentChannel.port,
      service_id: this.currentChannel.service_id,
      oscam: this.currentChannel.oscam};
    this.dvbConfigChannelForm.patchValue(this.defaultDvbConfgChannelFormValue);
  }

  trigger(eventName: string, ...args) {
    this[eventName].emit(...args);
    if (eventName in this.events) {
      this.events[eventName](...args);
    }
  }
  ngAfterViewInit(): void {
  }
  changeChannel() {
    console.log('change');
    if (this.dvbConfigChannelForm.valid) {
      this.trigger('success', this.dvbConfigChannelForm.value);
    }
  }
  ngOnDestroy(): void {
  }

}
